package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dto.CheckGroupDto;
import com.itheima.entity.PageReq;
import com.itheima.entity.PageRes;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckGroupItem;
import com.itheima.service.CheckGroupItemService;
import com.itheima.service.CheckGroupService;
import com.itheima.vo.CheckGroupVO;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CheckGroupServiceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupService {

    @Resource
    private CheckGroupItemService checkGroupItemService;

    @Override
    public boolean deleteById(Long id) {
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(id);
        checkGroup.setIs_delete(1);
        this.updateById(checkGroup);
        return true;
    }

    @Override
    public PageRes<CheckGroup> findByPage(PageReq req) {
        //条件构造器
        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<>();
        String queryString = req.getQueryString();
        // and is_delete = 0
        queryWrapper.eq("is_delete",0);
        if(queryString != null && "".equals(queryString.trim()) == false){
            // 拼接 name like '%'+ queryString + '%'
            queryWrapper.and(wrapper -> {
                wrapper.like("name",req.getQueryString()).or()
                        .like("code",req.getQueryString()).or()
                        .like("help_code",req.getQueryString());
            });
        }
        queryWrapper.orderByDesc("id");
        //E page, Wrapper<T> queryWrapper
        IPage<CheckGroup> result = this.page(new Page(req.getCurrentPage(),req.getPageSize()),queryWrapper);
        //分页结果
        return new PageRes<CheckGroup>(result.getTotal(),result.getRecords());
    }

    @Override
    @Transactional
    public boolean saveGroup(CheckGroupDto checkGroupDto) {
        //1.保存检查组信息
        this.saveOrUpdate(checkGroupDto.getCheckGroup());
        //2.清楚之前维护的关系数据
        checkGroupItemService.deleteByGroupId(checkGroupDto.getCheckGroup().getId());
        //3.维护检查组与检查项的关系
        if(checkGroupDto.getItemIds() != null){
            List<CheckGroupItem> list = new ArrayList<>();//批处理集合
            for(Long itemId : checkGroupDto.getItemIds()){
                CheckGroupItem checkGroupItem = new CheckGroupItem();
                checkGroupItem.setGroupId(checkGroupDto.getCheckGroup().getId());
                checkGroupItem.setItemId(itemId);
//                checkGroupItemService.saveOrUpdate(checkGroupItem);//逐条保存
                list.add(checkGroupItem);
            }
            checkGroupItemService.saveBatch(list);//批量插入
        }
        return true;
    }

    @Override
    public List<CheckGroupVO> selectBySetmealId(Long setmealId) {
        return this.baseMapper.selectBySetmealId(setmealId);
    }

}
